<template>
	<div class="zs-table-page-container">
		<SearchTabTime :tabList="tabList" :curTabIndex="curTabIndex" @onChange="changeTab" :tag="`${tag} 用量`"></SearchTabTime>
		<div class="zs-card zs-table-page-content" v-loading="loading">
			<ProjectTable v-if="curTabIndex > -1" :tableData="tableData" :updateDate="updateDate" :tag="tag" :tabPermi="tabList[curTabIndex].permi"></ProjectTable>
		</div>
	</div>
</template>
<script setup>
	import SearchTabTime from '@/DbComponents/searchTabTime';
	import ProjectTable from '../components/projectTable.vue';
	import dbPublicApi from '@/api/dbPublic';
	import { ref } from 'vue';
	import { useRoute } from 'vue-router';

	const route = useRoute();
	const tabList = ref([
		{ label: '商业', value: '商业', permi: 'p_btn_904_zhuanxiang_nyxh_shangye' },
		{ label: '产办', value: '产办', permi: 'p_btn_904_zhuanxiang_nyxh_chanban' },
		{ label: '酒寓', value: '酒寓', permi: 'p_btn_904_zhuanxiang_nyxh_jiuyu' },
		{ label: '餐饮', value: '餐饮', permi: 'p_btn_904_zhuanxiang_nyxh_canyin' },
	]);
	const curTabIndex = ref(-1);

	let tag = ref('');
	const loading = ref(true);
	const tableData = ref([]);
	const updateDate = ref('');
	const getDate = async menuKey => {
		await dbPublicApi.queryDataTimeApi(menuKey).then(res => {
			if (res.data.code == 200) {
				tag.value = res.data.message;
				updateDate.value = res.data.data;
			}
		});
	};
	const getList = async () => {
		await dbPublicApi.queryProjectPcApi({ dataTime: updateDate.value, resourceKey: tabList.value[curTabIndex.value].permi, pageKey: route.query.rid }).then(res => {
			if (res.data.code == 200) {
				tableData.value = res.data.data;
			}
		});
	};
	const getTabIndex = async () => {
		let list = await JSON.parse(localStorage.getItem('portalResourceList'));
		if (!list) {
			return false;
		}
		for (let [index, tab] of tabList.value.entries()) {
			if (list.indexOf(tab.permi) > -1) {
				curTabIndex.value = index;
				break;
			}
		}
	};
	const changeTab = async data => {
		loading.value = true;
		curTabIndex.value = data.data;
		tableData.value = [];
		await getDate(tabList.value[curTabIndex.value].permi);
		await getList();
		loading.value = false;
	};
	const init = async () => {
		await getTabIndex();
		if (curTabIndex.value == -1) {
			loading.value = false;
			return false;
		}
		await getDate(tabList.value[curTabIndex.value].permi);
		await getList();
		loading.value = false;
	};
	init();
</script>

<style scoped lang="scss"></style>
